package com.aegean.MonitorExamSYS.examinee.action;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.aegean.MonitorExamSYS.examinee.business.ExamineeBusiness;
import com.aegean.MonitorExamSYS.tool.business.ToolBusiness;
import com.aegean.MonitorExamSYS.tools.MyTools;
import com.aegean.MonitorExaxSYS.model.ConductionValue;
import com.aegean.MonitorExaxSYS.model.ExamInfo;
import com.aegean.MonitorExaxSYS.model.ExamineeValue;
import com.aegean.MonitorExaxSYS.model.MonitorValue;
import com.opensymphony.xwork2.ActionSupport;

@Controller("ExamApplyAction")
@Scope("prototype")
public class ExamApplyAction extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = 4187172620375066518L;
	@Resource
	ExamineeBusiness business;
	@Resource
	ToolBusiness business2;
	private List<ExamInfo> exams = new ArrayList<ExamInfo>();
	private List<ConductionValue> conductions;
	private int small_Id;
	private int start;
	private int limit;
	private int exam_Id;
	private ExamInfo exam;
	private boolean success;
	private String msg;
	private int conduction_Id;
	private int pageNow;// 当前页
	private int totalRows;// 总条例个数
	private int pageCount;// 页数
	public int getPageNow() {
		return pageNow;
	}

	public void setPageNow(int pageNow) {
		this.pageNow = pageNow;
	}

	public int getTotalRows() {
		return totalRows;
	}

	public void setTotalRows(int totalRows) {
		this.totalRows = totalRows;
	}

	public int getPageCount() {
		return pageCount;
	}

	public void setPageCount(int pageCount) {
		this.pageCount = pageCount;
	}

	public int getConduction_Id() {
		return conduction_Id;
	}

	public void setConduction_Id(int conduction_Id) {
		this.conduction_Id = conduction_Id;
	}

	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public ExamInfo getExam() {
		return exam;
	}

	public void setExam(ExamInfo exam) {
		this.exam = exam;
	}

	public List<ConductionValue> getConductions() {
		return conductions;
	}

	public void setConductions(List<ConductionValue> conductions) {
		this.conductions = conductions;
	}

	public int getExam_Id() {
		return exam_Id;
	}

	public void setExam_Id(int exam_Id) {
		this.exam_Id = exam_Id;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}
	public List<ExamInfo> getExams() {
		return exams;
	}

	public void setExams(List<ExamInfo> exams) {
		this.exams = exams;
	}

	public int getSmall_Id() {
		return small_Id;
	}

	public void setSmall_Id(int small_Id) {
		this.small_Id = small_Id;
	}

	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		try {
			limit = 15;
			totalRows = business.searchExamValueCount(small_Id);
			if ((totalRows % limit) == 0) {
				pageCount = totalRows / limit;
			} else {
				pageCount = totalRows / limit + 1;
			}
			if (pageNow > pageCount) {
				pageNow = pageCount;
			} else if (pageNow < 1) {
				pageNow = 1;
			}
			start = (pageNow - 1) * limit;
			exams = business.searchPartExamValues(small_Id, start, limit);
			return SUCCESS;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return ERROR;
		}
	}

	public String toApply() throws Exception {
		// TODO Auto-generated method stub
		try {
			exam = business2.searchExamInfo(exam_Id);
			ExamineeValue one = (ExamineeValue) ServletActionContext
					.getRequest().getSession().getAttribute("UserLogin");
			conductions = business.showConducation(exam_Id, one
					.getExamineeValue_Address().getPlace_H(), one
					.getExamineeValue_Address().getPlace_V());
			return SUCCESS;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return ERROR;
		}
	}

	public String applyForm() throws Exception {
		try {
			ExamineeValue one = (ExamineeValue) ServletActionContext
					.getRequest().getSession().getAttribute("UserLogin");
			success = business.applyQualification(conduction_Id,
					one.getUser_Id(), exam_Id);
			if (success == true) {
				msg = "恭喜"
						+ one.getUser_RealName()
						+ "考生申请成功，我们已经记录您需要报考的考试项目和希望的考试地点，我们会在最快的时间内给你分配考场和学号，请你注意查看个人中心考试考试信息！";
			} else {
				msg = "对不起，您的申请出现错误，可能你在申请的考试时间段已经申请过其他考试，导致时间冲突，请你认真检查，或者联系考试中心";
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return SUCCESS;
	}
}
